"""
This demo demonstrates how to embed a matplotlib (mpl) plot 
into a PyQt4 GUI application, including:

* Using the navigation toolbar
* Adding data to the plot
* Dynamically modifying the plot's properties
* Processing mpl events
* Saving the plot to a file from a menu

The main goal is to serve as a basis for developing rich PyQt GUI
applications featuring mpl plots (using the mpl OO API).

Eli Bendersky (eliben@gmail.com)
License: this code is in the public domain
Last modified: 19.01.2009
"""
import sys, os, random, time
from PyQt4.QtCore import *
from PyQt4.QtGui import *

import matplotlib
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar
from matplotlib.figure import Figure

class PlotGraphWidget(QWidget):
    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent)
        self.create_canvas()
    
    def on_pick(self, event):
        self.axes.annotate("test", (event.mouseevent.xdata,event.mouseevent.ydata,))
        self.canvas.draw()
        #box_points = event.artist.get_bbox().get_points()
        #msg = "You've clicked on a bar with coords:\n %s" % box_points
        #QMessageBox.information(self, "Click!", msg)
    
    def clear(self):
        self.axes.clear()
        self.axes.grid(True)
        
    def plot(self, x, y, plottype='bo'):
        """ Redraws the figure
        """
        self.axes.plot(x, y, plottype, picker=5)
        self.canvas.draw()
        
    
    def create_canvas(self):
        self.main_frame = QWidget()
        
        self.dpi = 100
        self.fig = Figure((5.0, 4.0), dpi=self.dpi)
        self.canvas = FigureCanvas(self.fig)
        self.canvas.setParent(self.main_frame)
        
        self.axes = self.fig.add_subplot(111)
        
        self.canvas.mpl_connect('pick_event', self.on_pick)
        
        vbox = QVBoxLayout()
        vbox.addWidget(self.canvas)
        
        self.setLayout(vbox)
    
